package rad.framework.jsf;

import java.util.ResourceBundle;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/**
 * This converter uses a resource bundle to calculate getAsString
 * 
 * @author boudyacho
 * 
 */
public class EnumOutputConverter implements Converter {

	public Object getAsObject(FacesContext context, UIComponent comp,
			String value) throws ConverterException {
		throw new UnsupportedOperationException();
	}

	public String getAsString(FacesContext ctx, UIComponent comp, Object object)
			throws ConverterException {
		// System.out.println("getAsString: " + object);
		if (object == null) {
			return null;
		}
		if (object instanceof String) {
			return object.toString();
		}
		Enum type = (Enum) object;
		return getString(type);
	}

	private String getString(Enum type) {
		ResourceBundle bundle = org.jboss.seam.core.ResourceBundle.instance();
		String key = type.getClass().getSimpleName() + "_" + type.name();
//		System.out.println("EnumOutputConverter.getString: " + key);
		String value = bundle.getString(key);
		if (value == null) {
			return "";
		}
		return value;
	}
}
